<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/22
 * Time: 20:37
 */
namespace App\Services\LandingRepaymentE;

class LandingRepaymentEService
{
    protected $landingEApi;
    protected $landingESecret;
    protected $landingEMerchno;
    protected $landingEConsumeNotifyUrl;
    protected $landingERepaymentNotifyUrl;
    protected $landingEWithdrawNotifyUrl;

    private $methodname;//接口请求业务类型
    private $orderNo;//订单号
    private $returnUrl;//跳转返回url
    private $notifyUrl;//回调url
    private $truename;//真实姓名
    private $realname;//真实姓名
    private $idCardNo;//身份证号
    private $mobile;//手机号码
    private $bankCardNo;//银行卡号
    private $subMerchantNo;//子商户号
    private $amount;//金额
    private $userFee;//用户费率 金额
    private $fixAmount;//用户费率 金额
    private $mcc;//消费类型编号
    private $bankName;//银行名称
    private $transtype;//交易类型
    private $accounttype;//交易类型
    private $accountProperty;//交易类型
    private $cvn2;//卡有效期
    private $expireDate;//交易类型
    private $province;//省份

    public function __construct()
    {
        $this->landingEApi = config('landingrepaymente.landing_e_api');//大额e接口
        $this->landingESecret = config('landingrepaymente.landing_e_secret');//商户密钥
        $this->landingEMerchno = config('landingrepaymente.landing_e_merchno');//商户id
        $this->landingEConsumeNotifyUrl = config('landingrepaymente.landing_e_consume_notify_url');//消费回调
        $this->landingERepaymentNotifyUrl = config('landingrepaymente.landing_e_repayment_notify_url');//还款回调
        $this->landingEWithdrawNotifyUrl = config('landingrepaymente.landing_e_withdraw_notify_url');//提现回调
    }

    /**
     * 请求参数
     * @return array
     */
    protected function requestParams($transcode = '015')
    {
        $params =  [
            'transcode' => $transcode,
            'version' => '0100',
            'merchno' => $this->landingEMerchno,
            'methodname' => $this->methodname,
            'dsorderid' => $this->orderNo,
            'returnUrl' => $this->returnUrl,
            'notifyUrl' => $this->notifyUrl,
            'accountName' => $this->truename,
            'username' => $this->realname,
            'idcard' => $this->idCardNo,//身份证号
            'mobile' => $this->mobile,
            'bankcard' => $this->bankCardNo,
            'accounttype' => $this->accounttype,
            'accountProperty' => $this->accountProperty,
            'bankName' => $this->bankName,
            'subMerchantNo' => $this->subMerchantNo,//子商户
            'amount' => $this->amount,
            'futureRateValue' => $this->userFee,
            'fixAmount' => $this->fixAmount,
            'mcc' => $this->mcc,
            'transtype' => $this->transtype,
            'cvn2' => $this->cvn2,
            'expireDate' => $this->expireDate,
            'province' => $this->province,
        ];
        if (null === $transcode)
            unset($params['transcode']);
        if (null === $this->methodname)
            unset($params['methodname']);
        if (null === $this->orderNo)
            unset($params['dsorderid']);
        if (null === $this->returnUrl)
            unset($params['returnUrl']);
        if (null === $this->notifyUrl)
            unset($params['notifyUrl']);
        if (null === $this->truename)
            unset($params['accountName']);
        if (null === $this->realname)
            unset($params['username']);
        if (null === $this->idCardNo)
            unset($params['idcard']);
        if (null === $this->mobile)
            unset($params['mobile']);
        if (null === $this->bankCardNo)
            unset($params['bankcard']);
        if (null === $this->bankName)
            unset($params['bankName']);
        if (null === $this->subMerchantNo)
            unset($params['subMerchantNo']);
        if (null === $this->amount)
            unset($params['amount']);
        if (null === $this->userFee)
            unset($params['futureRateValue']);
        if (null === $this->fixAmount)
            unset($params['fixAmount']);
        if (null === $this->mcc)
            unset($params['mcc']);
        if (null === $this->transtype)
            unset($params['transtype']);
        if (null === $this->accounttype)
            unset($params['accounttype']);
        if (null === $this->accountProperty)
            unset($params['accountProperty']);
        if (null === $this->cvn2)
            unset($params['cvn2']);
        if (null === $this->expireDate)
            unset($params['expireDate']);
        if (null === $this->province)
            unset($params['province']);
        return $params;
    }

    /**
     * 获取接口请求结果
     * @param $param
     * @param $url
     * @return mixed
     */
    protected function getRequestResult($param, $url)
    {
        // 根据key排序数组
        ksort($param);
        // 转换成字符串 格式为：key=valkey2=val2
        $singleStr = $this->toUrlParams($param);
        // 加密字符拼接秘钥进行md5加密，得到签名
        $param['sign'] = md5($singleStr . $this->landingESecret);
        $client = new \GuzzleHttp\Client(['verify' => false]);
        $response = $client->request('post', $url, ['json' => $param]);
        $json = json_decode($response->getBody()->getContents(), true);
        return $json;
    }

    /**
     * 格式化参数
     * @param $param
     * @return string
     */
    protected function toUrlParams($param)
    {
        $string = '';
        foreach ($param as $k=>$v){
            if(is_array($v)){
                $s = '';
                foreach ($v as $key=>$val){
                    $s .= $key.'=' . $val . ', ';
                }
                $s = substr($s,0,-2);
                $string .= $k . '=' . '{'. $s .'}';
            }else{
                $string .= $k . '=' . $v;
            }
        }
        return $string;
    }

    /**
     * 子商户注册
     * @param $orderNo
     * @param $trueName
     * @param $mobile
     * @param $idCard
     * @param $bankCardNo
     * @param $bankCardName
     * @param $consumeAddress
     * @return mixed
     */
    public function subMerchantRegister($orderNo, $trueName, $mobile, $idCard)
    {
        $this->orderNo = $orderNo;
        $this->methodname = 'register';
        $this->truename = $trueName;
        $this->mobile = $mobile;
        $this->idCardNo = $idCard;

        $dataContent = $this->requestParams();
        return $this->getRequestResult($dataContent, $this->landingEApi);
    }

    /**
     * 子商户余额查询
     * @param $subMerchantNo
     * @return mixed
     */
    public function subMerchantBalanceSelect($orderNo, $subMerchantNo)
    {
        $this->methodname = 'queryBalance';
        $this->orderNo = $orderNo;
        $this->subMerchantNo = $subMerchantNo;

        $dataContent = $this->requestParams();
        return $this->getRequestResult($dataContent, $this->landingEApi);
    }

    /**
     * 子商户消费
     * @param $orderNo
     * @param $subMerchantNo
     * @param $bankCardNo
     * @param $amount
     * @param $userFee
     * @param $mcc
     * @param $province
     * @return mixed
     */
    public function subMerchantConsume($orderNo, $subMerchantNo, $amount, $userFee, $fixAmount,
                                       $bankCardNo, $trueName, $mobile, $idCard, $cvn2, $expireDate, $province)
    {
        $this->methodname = 'pay';
        $this->orderNo = $orderNo;
        $this->subMerchantNo = $subMerchantNo;
        $this->amount = round($amount * 100, 2);
        $this->userFee = round($userFee * 100, 2);
        $this->fixAmount = round($fixAmount * 100, 2);
        $this->bankCardNo = $bankCardNo;
        $this->truename = $trueName;
        $this->mobile = $mobile;
        $this->idCardNo = $idCard;
        $this->cvn2 = $cvn2;
        $this->expireDate = $expireDate;
        $this->province = $province;
        $this->notifyUrl = $this->landingEConsumeNotifyUrl;

        $dataContent = $this->requestParams();
        return $this->getRequestResult($dataContent, $this->landingEApi);
    }

    /**
     * 子商户还款
     * @param $orderNo
     * @param $subMerchantNo
     * @param $bankCardNo
     * @param $amount
     * @param $userFee
     * @return mixed
     */
    public function subMerchantRepayment($orderNo, $subMerchantNo, $amount, $fixAmount,
                                         $bankCardNo, $mobile)
    {
        $this->methodname = 'withDraw';
        $this->orderNo = $orderNo;
        $this->subMerchantNo = $subMerchantNo;
        $this->amount = round($amount * 100, 2);
        $this->fixAmount = $fixAmount;
        $this->bankCardNo = $bankCardNo;
        $this->mobile = $mobile;
        $this->notifyUrl = $this->landingERepaymentNotifyUrl;

        $dataContent = $this->requestParams();
        return $this->getRequestResult($dataContent, $this->landingEApi);
    }

    /**
     * 子商户提现
     * @param $orderNo
     * @param $subMerchantNo
     * @param $bankCardNo
     * @param $amount
     * @param $userFee
     * @return mixed
     */
    public function subMerchantWithdraw($orderNo, $subMerchantNo, $amount, $fixAmount,
                                         $bankCardNo, $mobile)
    {
        $this->methodname = 'withDraw';
        $this->orderNo = $orderNo;
        $this->subMerchantNo = $subMerchantNo;
        $this->amount = round($amount * 100, 2);
        $this->fixAmount = $fixAmount;
        $this->bankCardNo = $bankCardNo;
        $this->mobile = $mobile;
        $this->notifyUrl = $this->landingEWithdrawNotifyUrl;

        $dataContent = $this->requestParams();
        return $this->getRequestResult($dataContent, $this->landingEApi);
    }
}